import 'package:changxindianzishangwu/else/lib/common/index.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/country_phone.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:changxindianzishangwu/util/app_color.dart';


///选择区号
class CountryPhonePage extends StatefulWidget {
  CountryPhonePage({Key key}) : super(key: key);

  @override
  _CountryPhonePageState createState() => _CountryPhonePageState();
}

class _CountryPhonePageState extends State<CountryPhonePage> {
  var countryPhone;
  FocusNode focusNode = new FocusNode();

  List countryData=[];

  @override
  void initState() {
    super.initState();
    countryData.addAll(country);
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        print(1);
      } else {
        countryContainer();
        print(2);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "选择区号"),
      body: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Stack(
          children: [
            search(),
            Padding(
              padding: const EdgeInsets.only(top: 50),
              child: countryContainer(),
            ),
          ],
        ),
      ),
    );
  }


  Container search() {
    return Container(
      margin: EdgeInsets.only(
          left: width(15), right: width(15), bottom: height(15)),
      child: TextField(
        maxLength: 10,
        maxLines: 1,
        cursorColor: AppColor.colorTheme,
        keyboardType: TextInputType.text,
        style: TextStyle(color: AppColor.colorText85, fontSize: 15),
        decoration: InputDecoration(
            fillColor: AppColor.bg,
            filled: true,
            counterText: "",
            contentPadding: EdgeInsets.only(left: 15, right: 15),
            hintText: "请输入搜索内容...",
            hintStyle: TextStyle(color: AppColor.colorHintText, fontSize: 15),
            enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Color(0x00FF0000)),
                borderRadius: BorderRadius.all(Radius.circular(5))),
            border: OutlineInputBorder(
                borderSide: BorderSide(color: Color(0x00000000)),
                borderRadius: BorderRadius.all(Radius.circular(5)))),
        onEditingComplete: () {
          setState(() {
            FocusScope.of(context).requestFocus(FocusNode());
          });
        },
        textInputAction: TextInputAction.done,
        onSubmitted: (str) {
          print('------输入内容:$str');
          if(ObjectUtil.isEmptyString(str)){
            if (mounted)
              setState(() {
                countryData.clear();
                countryData.addAll(country);
              });
            return;
          }
          int i = _searchCountry(str);
          if (i == -1) {
            toast("暂无该国家区号");
            return;
          }
          if (mounted)
            setState(() {
              countryData.clear();
              countryData.add(country[i]);
            });
        },
      ),
    );
  }

  int _searchCountry(cns) {
    for (int i = 0; i < country.length; i++) {
      if (cns == country[i]['cn']) {
        print(i);
        return i;
      }
    }
    return -1;
  }

  Container countryContainer() {


    print('-----内容：${countryData.length}');
    return Container(
      child: ListView.builder(
          itemCount: countryData.length,
          itemBuilder: (context, index) {
            return InkWell(
              onTap: () {
                print(countryData[index]["code"].toString());
                // Get.offAllNamed("/loginInit",
                //     arguments: countryData[index]["code"].toString());
                String a = countryData[index]["code"].toString();
                print('-------区号：$a');
                Get.back(result: a);
              },
              child: ListTile(
                leading: Text(countryData[index]["cn"].toString()),
                trailing: Text(countryData[index]["code"].toString()),
              ),
            );
          }),
    );
  }
}
